package juhouse.glb.manage.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import juhouse.framework.tools.DoText;
import juhouse.glb.manage.dao.GradeDao;
import juhouse.glb.manage.dao.MenuDao;
import juhouse.glb.manage.dao.MenuSortDao;
import juhouse.glb.manage.form.MenuForm;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class MenuAction extends DispatchAction{
	public ActionForward menuList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			this.menuListPage(request);
		}
		return mapping.findForward("menuList");
	}
	private void menuListPage(HttpServletRequest request)
	{
		int sortId  = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
		MenuDao dao  = new MenuDao();
		String[][] menuList = dao.getMenuList(sortId);
		request.setAttribute("menuList",menuList);
		this.menuSortList(request);
	}
	private void menuSortList(HttpServletRequest request)
	{
		int sortId      = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
		MenuSortDao dao = new MenuSortDao();
		String[][] menuSortList = dao.getMenuSortList(sortId);
		request.setAttribute("menuSortList",menuSortList);
	}
	public ActionForward newMenu(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			this.getGradeList(request);
			this.menuSortList(request);
			saveToken(request);
		}
		return mapping.findForward("menuQuery");
	}
	
	public void getGradeList(HttpServletRequest request){
		GradeDao gdao = new GradeDao();
		String[][] gradeList = gdao.getGradeList();
		request.setAttribute("gradeList", gradeList);
	}
	
	public ActionForward modifyMenu(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			MenuDao dao = new MenuDao();
			int sortId = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
			int menuId = NumberUtils.stringToInt(request.getParameter("menuId"), 0);
			String[][] menuList = dao.getMenuList(sortId,menuId);
			request.setAttribute("menuList",menuList);
			this.menuSortList(request);
			this.getGradeList(request);
			saveToken(request);
		}
		return mapping.findForward("menuQuery");
	}
	public ActionForward saveMenu(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MenuForm menuForm = (MenuForm)form;
		if(request.getSession().getAttribute("manageId")!=null)
		{
			MenuDao dao = new MenuDao();
			int menuId = menuForm.getMenuId();
			if(menuId==0)
			{
				if (isTokenValid(request, true)) 
				{
					dao.newSaveMenu(menuForm);
				}
			}else{
				if (isTokenValid(request, true)) 
				{
					dao.modifySaveMenu(menuForm);
				}
			}
			this.menuListPage(request);
		}
		return mapping.findForward("menuList");
	}
	public ActionForward deleteMenu(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if(request.getSession().getAttribute("manageId")!=null)
		{
			int sortId = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
			int menuId = NumberUtils.stringToInt(request.getParameter("menuId"), 0);
			MenuDao dao = new MenuDao();
			dao.deleteMenu(sortId,menuId);
			this.menuListPage(request);
		}
		return mapping.findForward("menuList");
	}
	public ActionForward moveMenu(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int sortId           = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
		int menuId           = NumberUtils.stringToInt(request.getParameter("menuId"), 0);
		int oldDOrder           = NumberUtils.stringToInt(request.getParameter("oldDOrder"), 0);
		int dOrder           = NumberUtils.stringToInt(request.getParameter("dOrder"), -1000);
		if(dOrder!=-1000)
		{
			MenuDao dao = new MenuDao();
			dao.moveMenu(sortId,menuId,dOrder,oldDOrder);
		}
		this.menuListPage(request);
		return mapping.findForward("menuList");
	}
	public ActionForward menuValidate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		MenuDao dao = new MenuDao();
		int sortId   = NumberUtils.stringToInt(request.getParameter("sortId"), 0);
		int menuId   = NumberUtils.stringToInt(request.getParameter("menuId"), 0);
    	String menu = request.getParameter("menu");
    	DoText myDoText = new DoText();
    	menu = myDoText.iso2gb(menu);
        if(dao.menuValidate(sortId,menuId,menu))
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	request.setAttribute("validateResult", "false");
        }
		return mapping.findForward("validateResult");
	}
}